<?php

 include 'connectDB.php';

 $fun = $_POST["function"];
// $tid = $_POST["merid"];

 if($fun=='add'){
	add();
 }
 else if($fun=='del'){
 	del();
 }
 elseif ($fun=='showEdit') {
   showEdit();
 }
 else if($fun=='loadList'){
   loadList();
 }

 else if($fun=='saveEdit'){
 	saveEdit();
 }


function add(){
	$strSQL = "INSERT INTO tbl_merchant( mer_name, description,longtitude, latitude, date)
	VALUES ( '".$_POST[mername]."' , '".$_POST[desc]."', '".$_POST[longti]."', '".$_POST[lati]."', now()) ";

	$objQuery = mysql_query($strSQL);
	loadList();
	//mysql_close($objConnect);
 }

function del(){
	$strSQL = " DELETE FROM tbl_merchant WHERE mer_id = '".$_POST[mer_id]."' ";
	$objQuery = mysql_query($strSQL);
    echo 'Delete Success.';
}

function saveEdit(){
    $mer_id 	 = $_POST["mer_id"];
	$mer_name 	 = $_POST["mer_name"];
	$description = $_POST["description"];
	$longtitude  = $_POST["longtitude"];
	$latitude 	 = $_POST["latitude"];

    $strSQL = " UPDATE tbl_merchant SET mer_name ='".$mer_name."' , description = '".$description."',
			    longtitude  = '".$longtitude."', latitude	= '".$latitude."' WHERE mer_id = '".$mer_id."' ";
    $objQuery = mysql_query($strSQL);
   // echo 'Edit Success.';
	loadList();
}

function showEdit(){

    $mer_id = $_POST["mer_id"];
    $strSQL = "SELECT * FROM tbl_merchant WHERE  mer_id = '".$mer_id."' ";
    $objQuery = mysql_query($strSQL);


}
function loadList(){
?>
	<table border="0" cellpadding="10" cellspacing="5">
	  	<tr>
	  		<td>Merchant ID</td>
	  		<td>Merchant Name</td>
	  		<td>Description</td>
	  		<td>Longtitude</td>
	  		<td>Latitude</td>
	  		<td>Date</td>
	  		<td>E</td>
	  		<td>D</td>
	  	</tr>
     <?php
	 $strSQL = "SELECT * FROM tbl_merchant ORDER BY date DESC";
	 $objQuery = mysql_query($strSQL);

		while ($objResult = mysql_fetch_array($objQuery))
		{
	 ?>

	  	<tr>
	  		<td><?php echo $objResult['mer_id'];?></td>
	  		<td><?php echo $objResult['mer_name'];?></td>
	  		<td><?php echo $objResult['description'];?></td>
	  		<td><?php echo $objResult['longtitude'];?></td>
	  		<td><?php echo $objResult['latitude'];?></td>
	  		<td><?php echo $objResult['date'];?></td>

	  	<!--<td><a href="JavaScript:if(confirm('Comfirm Edit Data?')==true){window.location='EditMerchant.php?MerID=<?=$objResult["mer_id"];?>';}">Edit</a></td> -->
	 		<td><input type="button" name="edit-btn" id="edit-btn" value="edit" onclick="editMer(
 <?=$objResult["mer_id"];?>,'<?=$objResult["mer_name"];?>','<?=$objResult["description"];?>','<?=$objResult["longtitude"];?>',' <?=$objResult["latitude"];?>');"  /></td>
	  	   <td><input type="button" name="del-btn" id="del-btn"  onclick="delMer(<?=$objResult["mer_id"];?>)" value="del" /></td>

	  	</tr>
	<?php
		}
	?>
    </table>
	<?php
}
	?>
